/*
 * Ext JS Library 2.2
 * Copyright(c) 2006-2008, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */
 Ext.namespace('Ext.relatedResearchData');
var importArticles = function(){
	
	
	Ext.QuickTips.init();
	function showSpinner(){
	    Ext.MessageBox.show({
           msg: '提示:数据正在导入中，请稍候...',
           progressText: '',
           width:300,
           wait:true,
           waitConfig: {interval:200},
           icon:'ext-mb-download'//custom class 
       });
	}
	
	
	function doJSON(stringData) {
		try {
			var jsonData = Ext.util.JSON.decode(stringData);
			Ext.MessageBox.alert('Success', jsonData.msg);
			
		}
		catch (err) {
			Ext.MessageBox.alert('ERROR', 'Could not decode ' + stringData);
		}
	}
	function importWordConfirm(btn, text){
       if(btn=='yes'){
       		doAjax();
       }
    }
   	function doAjax() {
   		Ext.Ajax.on('beforerequest', showSpinner, this);
		Ext.Ajax.request({
			url : 'importarticle.action' , 
			method: 'GET',
			success: function ( result, request) { 
				Ext.MessageBox.getDialog().close();
				doJSON(result.responseText);
			},
			failure: function ( result, request) { 
				Ext.MessageBox.alert('Failed', 'Successfully posted form: '+result.failure); 
			} 
		});
	}

	var jsonStore = new Ext.data.JsonStore({
		url:'articleaction.action',
		root:'articles',
        totalProperty: 'totalCount',
        idProperty: 'id',		
        fields: [
           {name: 'id'},
           {name: 'author'},
           {name: 'title'},
           {name: 'suffix'},
           {name:'type'},
           {name:'subContent'},
           {name: 'publishDate', type: 'date', dateFormat: 'Y-n-j'}
        ]
	});
    var pagingBar = new Ext.PagingToolbar({
        pageSize: 25,
        store: jsonStore,
        displayInfo: true,
        displayMsg: '当前显示数据： {0} - {1} ，总数 ：{2}',
        emptyMsg: "当前没有数据",
        
        items:[
            '-', {
            pressed: true,
            enableToggle:true,
            text: '显示上次数据',
            cls: 'x-btn-text-icon details',
            toggleHandler: function(btn, pressed){
                var view = grid.getView();
                view.showPreview = pressed;
                view.refresh();
                Ext.MessageBox.getDialog().close();
            }
        }]
    });
    // create the Grid
    var grid = new Ext.grid.GridPanel({
        store: jsonStore,
        columns: [
        	{header:'编号',width:30,sortable:true,dataIndex:'id'},
            {id:'title',header: "文章名称", width: 100, sortable: true, dataIndex: 'title'},
            {header: "后缀",  sortable: true, dataIndex: 'suffix'},
            {header: "类型",  width: 100,sortable: true,  dataIndex: 'type'},
            {header: "发布时间", width: 85, sortable: true,  dataIndex: 'publishDate',renderer: Ext.util.Format.dateRenderer('Y-n-j')}
        ],
        stripeRows: true,
        loadMask: true,
        autoExpandColumn: 'title',
        height:350,
        width:800,
        // paging bar on the bottom
        bbar: pagingBar,
		sm: new Ext.grid.RowSelectionModel({singleSelect: true}),
		viewConfig: {
			forceFit: true
		},                
		split: true,
		region: 'north'        
    });

// define a template to use for the detail view
	var bookTplMarkup = [
		'编号:{id}<br>',
		'标题: <a href="articledownload.action?id={id}" target="_blank">{title}</a><br/>',
		'后缀:{suffix}<br>',
		'所属类别:{type}<br>',
		'作者: {author}<br/>',
		'发表时间: {publishDate}<br/>',
		'摘要: <p>{subContent}</p>'
	];
	var bookTpl = new Ext.Template(bookTplMarkup);

	var ct = new Ext.Panel({
		renderTo: 'binding-example',
		frame: true,
		title: '代与代际关系研究',
		width: 840,
		height: 600,
		layout: 'border',
		tools:[{
		    id:'toggle',
		    qtip: 'Refresh form Data',
		    // hidden:true,
		    handler: function(event, toolEl, panel){
		        panel.toggleCollapse(true);
		    }
		},{
			id:'refresh',
			qtip:'刷新数据',
			handler:function(event,toolEl,panel){
				jsonStore.reload();
				Ext.MessageBox.getDialog().close();
			}
		}
		,{
			id:'gear',
			qtip:'更新研究成果',
			handler:function(event,toolEl,panel){
				var msg='接下来将把研究成果重新导入到数据库中，这是一个耗时较长的工作，并有可能将覆盖硬盘上已有的研究成果，你确认操作么？';
				if(Ext.isIE){
					 Ext.MessageBox.confirm('Confirm',msg , importWordConfirm);
				}else{
					if(confirm('接下来将把研究成果重新导入到数据库中，这是一个耗时较长的工作，并有可能将覆盖硬盘上已有的研究成果，你确认操作么？')){
								Ext.Ajax.request({
									url : 'importarticle.action' , 
									method: 'GET',
									success: function ( result, request) { 
											var stringData=result.responseText;
											var jsonData = Ext.util.JSON.decode(stringData);
											alert(jsonData.msg);
									},
									failure: function ( result, request) { 
										alert('Successfully posted form: '+result.failure); 
									} 
								});
					}
				}
				 
			}
		}
		],
		items: [
			grid,
			{
				id: 'detailPanel',
				region: 'center',
				bodyStyle: {
					background: '#ffffff',
					padding: '7px'
				},
				html: '点击表格中的行，查看对应的行数据'
			}
		]
	});
	grid.getSelectionModel().on('rowselect', function(sm, rowIdx, r) {
		var detailPanel = Ext.getCmp('detailPanel');
		bookTpl.overwrite(detailPanel.body, r.data);
	});
	 // render it
    grid.render();
    // trigger the data store load
    jsonStore.load({params:{start:0, limit:25}});
};